package com.example.oss.controller;

import com.example.oss.annotation.AccessLimit;
import com.example.oss.annotation.OperationLog;
import com.example.oss.annotation.RefreshLimit;
import com.example.oss.entity.*;
import com.example.oss.service.CourseService;
import com.example.oss.util.UserContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Random;

@Controller
@Slf4j
public class CourseController {

    @Autowired
    private CourseService courseService;

    @OperationLog("查看可选课程列表")
    @GetMapping("/listAble")
    //1秒内最多点击一次
//    @RefreshLimit(second = 1, maxCount = 1)
    //最大支持1分钟内3000个人访问
//    @AccessLimit(maxCount = 3000)
    public String getCourseListAble(Model model, Page page) {
        int i = new Random().nextInt(5) + 1;
        CourseResponse courseResponse = courseService.getAbleCourseList(i,
                page.getLimit());
        //数据总条数
        page.setTotalSize(courseResponse.getTotalSize());
        page.setPath("/listAble");
        //选课列表
        model.addAttribute("courses", courseResponse.getCourseList());
        model.addAttribute("loginUser", UserContextHolder.getUser());
        model.addAttribute("flag", 0);
        return "list";
    }

    @OperationLog("查看全部课程列表")
    @GetMapping("/listAll")
    //4秒内最多点击一次
    @RefreshLimit(second = 4, maxCount = 1)
    //最大支持1分钟内3000个人访问
    @AccessLimit(maxCount = 3000)
    public String getCourseListAll(Model model, Page page) {
        CourseResponse courseResponse = courseService.getAllCourseList(page.getPage(),
                page.getLimit());
        //数据总条数
        page.setTotalSize(courseResponse.getTotalSize());
        page.setPath("/listAll");
        //选课列表
        model.addAttribute("courses", courseResponse.getCourseList());
        model.addAttribute("loginUser", UserContextHolder.getUser());
        model.addAttribute("flag", 1);
        return "list";
    }


    @OperationLog("点击选课")
    @PostMapping("/doSelect")
    @ResponseBody
    //4秒内最多点击一次
//    @RefreshLimit(second = 4, maxCount = 1)
    //最大支持1分钟内1000个人选课
//    @AccessLimit(maxCount = 1000)
    public Result selectCourse() {
        int courseId = new Random().nextInt(30) + 1;
        int stuId = new Random().nextInt(1500000);
//        Student student = UserContextHolder.getUser();
//        if (student == null) {
//            return Result.failure(ResultEnum.NOT_LOGIN);
            return courseService.doSelectFromDB(courseId, stuId);
//        }
//        return courseService.doSelect(courseId, student.getId());
    }

    @OperationLog("查看我的选课")
    @GetMapping("/selectedCourse")
    //4秒内最多点击一次
    @RefreshLimit(second = 4, maxCount = 1)
    public String selectedCourse(Model model) {
        model.addAttribute("selectedCourse", courseService.getMySelectedCourseInfo());
        model.addAttribute("loginUser", UserContextHolder.getUser());
        return "result";
    }

    @OperationLog("获取选课结果")
    @GetMapping("/getSelectResult")
    @ResponseBody
    public Result getSelectResult(int courseId) {
        Student student = UserContextHolder.getUser();
        return courseService.getSelectResult(courseId, student.getId());
    }

    @OperationLog("退课")
    @PostMapping("/dropCourse")
    @ResponseBody
    //4秒内最多点击一次
    @RefreshLimit(second = 4, maxCount = 1)
    public void dropCourse(int courseId) {
        Student student = UserContextHolder.getUser();
        courseService.dropCourse(courseId, student.getId());
    }
}
